import java.util.Iterator;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;


 public class AccountApp {

    public static void main(String[] args) throws Exception {

    	// deberiamos registar el driver de mysql, para evitar
    	// problemas.
    	
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations

            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
        }
        
        
//        try {
//            Connection conn = DriverManager.getConnection("jdbc:mysql://pasarela.lab.inf.uva.es:33911","padawan","padawan");
//            // Do something with the Connection
//
//           
//        } catch (SQLException ex) {
//            // handle any errors
//            System.out.println("SQLException: " + ex.getMessage());
//            System.out.println("SQLState: " + ex.getSQLState());
//            System.out.println("VendorError: " + ex.getErrorCode());
//        }
        
    	
        // this uses h2 by default but change to match your database
        String databaseUrl = "jdbc:mysql://pasarela.lab.inf.uva.es:33911/ORMLite";
        // create a connection source to our database
        ConnectionSource connectionSource =
            new JdbcConnectionSource(databaseUrl);
        
       ((JdbcConnectionSource) connectionSource).setUsername("padawan");
       ((JdbcConnectionSource) connectionSource).setPassword("padawan");

        // instantiate the dao
        Dao<Account, String> accountDao =
            DaoManager.createDao(connectionSource, Account.class);
        
        Dao<ObjetoTeste, String> ObjTesteDao =
                DaoManager.createDao(connectionSource, ObjetoTeste.class);
        
        Dao<Usuario, String> UsuarioDao =
                DaoManager.createDao(connectionSource, Usuario.class);

        // if you need to create the 'accounts' table make this call
        //TableUtils.dropTable(connectionSource, Account.class, true);
        //TableUtils.dropTable(connectionSource, ObjetoTeste.class, true);
//        TableUtils.dropTable(connectionSource, Usuario.class, true);
        
        TableUtils.createTableIfNotExists(connectionSource, Usuario.class);
        //TableUtils.createTableIfNotExists(connectionSource, Account.class);
        //TableUtils.createTableIfNotExists(connectionSource, ObjetoTeste.class);
        
//        ObjetoTeste obj = new ObjetoTeste("Nombre"+Math.random());
//        ObjTesteDao.create(obj);
//
//
//          // create an instance of Account
//        Account account = new Account();
//        account.setName("Diego Gonzalez"+Math.random()*100);
//        account.setPassword(Math.random()*1000000+"");
//        account.setObj(obj);
//
//        // persist the account object to the database
//        accountDao.create(account);
        

//        Usuario u = new Usuario();
//        u.setNombre("jpsan");
//        u.setMail("mail@mail.com");
//        u.setRol("Desarrollador");
//        u.setPass(PasswordHash.createHash("jpsan"));
//        
//        UsuarioDao.create(u);
        
        // retrieve the account from the database by its id field (name)
        Usuario usuario;
        
        Iterator<Usuario> it = UsuarioDao.queryForAll().iterator();
        while(it.hasNext()){
        	
        	usuario = it.next();
        	
        	System.out.println(usuario.toString());
        	System.out.println(PasswordHash.validatePassword("jpsan", usuario.getPass()));
        }

        // close the connection source
        connectionSource.close();
    }
}
 
